package com.sdyeda.readiot.api;

import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class CallApi {

	static public byte[] MakeSender(String jarnpath, String classname, String id) {
		URL url;
		try {
			url = new File(jarnpath).toURI().toURL();
			@SuppressWarnings("resource")
			Class<?> clazz = new URLClassLoader(new URL[] { url }).loadClass(classname);
			// Class<?> clazz = classLoader.loadClass(classname);
			Object instance = clazz.getDeclaredConstructor().newInstance();
			Method method = clazz.getMethod("MakeSendBuff", String.class);
			return (byte[]) method.invoke(instance, (Object) id);

		} catch (Exception e) {
			return null;

		}

	}
	
	static public byte[] MakeSender(String jarnpath, String classname, String id,String command) {
		URL url;
		try {
			url = new File(jarnpath).toURI().toURL();
			@SuppressWarnings("resource")
			Class<?> clazz = new URLClassLoader(new URL[] { url }).loadClass(classname);
			// Class<?> clazz = classLoader.loadClass(classname);
			Object instance = clazz.getDeclaredConstructor().newInstance();
			Method method = clazz.getMethod("MakeSendBuff", String.class,String.class);
			return (byte[]) method.invoke(instance, (Object) id,command);

		} catch (Exception e) {
			return null;

		}
	}

	@SuppressWarnings("resource")
	static public List<String> FindClass(String jarnpath) {

		List<String> result = new ArrayList<String>();
		try (JarFile jarFile = new JarFile(jarnpath)) {
			Enumeration<JarEntry> entries = jarFile.entries();
			URL url = new File(jarnpath).toURI().toURL();

			while (entries.hasMoreElements()) {
				JarEntry entry = entries.nextElement();

				if (!entry.isDirectory() && entry.getName().endsWith(".class")) {
					String classname = entry.getName().replace("/", ".");
					//System.out.println(classname);
					classname = classname.substring(0, classname.indexOf(".class"));
					Class<?> clazz =null;
					try {
						clazz = new URLClassLoader(new URL[] { url }).loadClass(classname);
					}catch(Exception e) {
						continue;
					}
					//System.out.println(clazz.isInterface());
					// System.out.println("Class found in JAR file: " + entry.getName());
					if (!clazz.isInterface()) {
						Method[] methods = clazz.getMethods();
						Boolean found = false;
						for (int i = 0; i < methods.length; i++) {
							if (methods[i].getName().equals("MakeSendBuff")) {
								found = true;
							}
						}

						if (found) {
							//System.out.println("Found:" + classname);
							result.add(classname);
						}
					}
				}
			}
		} catch (Exception e) {
			//e.printStackTrace();
		}

		return result;

	}

	public static String DecRecive(String jarnpath, String classname,byte[] recive) {
		URL url;
		try {
			url = new File(jarnpath).toURI().toURL();
			@SuppressWarnings("resource")
			Class<?> clazz = new URLClassLoader(new URL[] { url }).loadClass(classname);
			// Class<?> clazz = classLoader.loadClass(classname);
			Object instance = clazz.getDeclaredConstructor().newInstance();
			Method method = clazz.getMethod("DecodeToJson", byte[].class);
			return (String) method.invoke(instance, (Object) recive);

		} catch (Exception e) {
			return null;
		}
	}

}
